package com.china08.yxyapi.service.mg.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.entity.mg.mistakes.TeaMistakesQues;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.repository.mg.mistakes.TeaMistakesQuesRepository;
import com.china08.yxyapi.service.mg.TeaMistakesQuesService;

@Service
public class TeaMistakesQuesServiceImpl implements TeaMistakesQuesService {

    @Autowired
    private HomeworkTaskReportQuesRepository homeworkTaskReportQuesRepository;

    @Autowired
    public MongoOperations mongoOperations;

    @Autowired
    private HomeworkTaskReportRepository homeworkTaskReportRepository;

    @Autowired
    private TeaMistakesQuesRepository teaMistakesQuesRepository;

    /**
     * 开始统计
     */
    @Override
    public void teaMistakesQuesStatistics() {
        getTaskIdforStatusOfFinished();
    }

    /**
     * 根据同步状态获取作业任务 （获取已批阅的任务）
     */
    @Override
    public void getTaskIdforStatusOfFinished() {
        for (;;) {
            Criteria criteria = new Criteria();
            // 查询条件
            // 同步状态
            criteria.and("statusOfFinished").is(2);
            criteria.andOperator(new Criteria().orOperator(Criteria.where("synchTeaMistakes").is(0),
                    Criteria.where("synchTeaMistakes").is(null)));
            // 错题状态

            Query query = new Query();
            query.addCriteria(criteria);

            query.with(new Sort(new Order(Direction.DESC, "createdDate")));
            query.limit(1000);
            List<HomeworkTaskReport> dataList = mongoOperations.find(query, HomeworkTaskReport.class);
            if (dataList.size() <= 0) {
                break;
            }
            for (HomeworkTaskReport htr : dataList) {
                forMistakesQues(htr.getId());
                htr.setSynchTeaMistakes(1);
                homeworkTaskReportRepository.save(htr);
            }
        }
    }

    /**
     * 循环错题
     */
    @Override
    public void forMistakesQues(String reportId) {
        Criteria criteria = new Criteria();
        // 查询条件
        // 同步状态
        criteria.and("reportId").is(reportId);
        // 查询为题型为选择题 且passed为0
        Criteria criteriaOrForAnd = new Criteria();
        criteriaOrForAnd.andOperator(Criteria.where("passed").is(0), new Criteria()
                .orOperator(Criteria.where("questionType").is("0"), Criteria.where("questionType").is("1")));

        criteria.andOperator(
                new Criteria().orOperator(Criteria.where("synchTeaMistakes").is(0),
                        Criteria.where("synchTeaMistakes").is(null)),
                new Criteria().orOperator(criteriaOrForAnd, Criteria.where("mark").lt(5)));
        // 错题状态

        Query query = new Query();
        query.addCriteria(criteria);

        query.with(new Sort(new Order(Direction.DESC, "createdDate")));
        List<HomeworkTaskReportQues> dataList = mongoOperations.find(query, HomeworkTaskReportQues.class);
        synchMistakesQues(dataList);
    }

    /**
     * 同步错题数据
     */
    @Override
    public void synchMistakesQues(List<HomeworkTaskReportQues> dataList) {
        for (HomeworkTaskReportQues htrq : dataList) {
            String id = htrq.getPublishUserId() + htrq.getClassId() + htrq.getQuesId();
            TeaMistakesQues tmq = teaMistakesQuesRepository.findOne(id);
            if (tmq != null) {
                // 知识点id合并
                List<Integer> knowIdList = tmq.getKnowIdLst();
                if (htrq.getKnowIdLst() != null) {
                    for (Integer knowId : htrq.getKnowIdLst()) {
                        if (!knowIdList.contains(knowId)) {
                            knowIdList.add(knowId);
                        }
                    }
                    tmq.setKnowIdLst(knowIdList);
                }
                // 知识点名字合并
                if (htrq.getKnowNameLst() != null) {
                    List<String> knowNameList = tmq.getKnowNameLst();
                    for (String knowName : htrq.getKnowNameLst()) {
                        if (!knowNameList.contains(knowName)) {
                            knowNameList.add(knowName);
                        }
                    }
                    tmq.setKnowNameLst(knowNameList);
                }
                tmq.setStatus(0);
                tmq.setMistakesNumber(tmq.getMistakesNumber() + 1);
            } else {
                tmq = new TeaMistakesQues();
                tmq.setId(id);
                tmq.setClassId(htrq.getClassId());
                tmq.setQuesId(htrq.getQuesId());
                List<String> sources = new ArrayList<String>();
                sources.add("00");
                tmq.setSources(sources);
                tmq.setPublishUserId(htrq.getPublishUserId());
                tmq.setPublishUserNick(htrq.getPublishUserNick());
                tmq.setSchoolId(htrq.getSchoolId());
                tmq.setStage(htrq.getStage());
                tmq.setSubject(htrq.getSubject());
                tmq.setQuestionType(htrq.getQuestionType());
                tmq.setKnowIdLst(htrq.getKnowIdLst());
                tmq.setKnowNameLst(htrq.getKnowNameLst());
                tmq.setStatus(0);
                tmq.setMistakesNumber(1);
            }
            htrq.setSynchTeaMistakes(1);
            tmq.setCreatedDate(new Timestamp(new Date().getTime()));
            homeworkTaskReportQuesRepository.save(htrq);
            teaMistakesQuesRepository.save(tmq);
        }
    }
}
